package cn.edu.nju.software.gui;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import cn.edu.nju.software.net.Connection;
import cn.edu.nju.software.net.CourseBean;
import cn.edu.nju.software.net.StudentBean;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class CourseSelection extends JPanel implements ActionListener{
	Connection connection; 
	StudentsOperation so;
	ArrayList<ArrayList<CourseBean>> allCourses; //embeded ArrayList
	ArrayList<CourseBean> unChosenCourses = new ArrayList<CourseBean>();//allCourses.get(0)
	ArrayList<CourseBean> chosenCourses = new ArrayList<CourseBean>();//allCourses.get(1)
    ArrayList<CourseBean> coursesToChoose = new ArrayList<CourseBean>();//coursesToChoose
    ArrayList<Integer> tempForSelect = new ArrayList<Integer>();
   
    
	private JTable coursesSelected;
	private JTable coursesUnSelected;
	private JTable coursesToSelect;
	
	public CourseSelection(StudentsOperation soIn){
		super();
		so = soIn;
		connection = Connection.getInstance();
		
		allCourses = connection.handleCourseView(so.getUsername());
		unChosenCourses = allCourses.get(0);
		chosenCourses = allCourses.get(1);
		
		
		int rowOfunchosen = unChosenCourses.size();
		int rowOfchosen = chosenCourses.size();
		
		coursesSelected = new JTable();//
		coursesUnSelected = new JTable();
		coursesToSelect = new JTable();
		JScrollPane pane1 = new JScrollPane (coursesSelected);
		JScrollPane pane2 = new JScrollPane (coursesUnSelected);
		JScrollPane pane3 = new JScrollPane (coursesToSelect);
		
		 final DefaultTableModel cs_dtm  = (DefaultTableModel)coursesSelected.getModel() ; 
		  coursesSelected.setModel(cs_dtm); 
		  final Object[] columnNames = {"id", "name", "institute",//���������final����
				   "teacher", "grade", "number", "place", "shared"};
		  cs_dtm.setColumnIdentifiers(columnNames); 
		 
		  for(int i = 1;i<=rowOfchosen;i++){
		        
		        cs_dtm.addRow(new Object[] {
		        		chosenCourses.get(i-1).getId(),
		        		chosenCourses.get(i-1).getName(),
		        		chosenCourses.get(i-1).getInstitute(),
		        		chosenCourses.get(i-1).getTeacher(),
		        		chosenCourses.get(i-1).getGrades(),
		        		chosenCourses.get(i-1).getNumber(),
		        		chosenCourses.get(i-1).getPlace(),
		        		chosenCourses.get(i-1).getShared(),
		        		});   
		          }//display the selected courses
		  cs_dtm.fireTableDataChanged();//repaint the table
		  
		  final DefaultTableModel cu_dtm  = (DefaultTableModel)coursesUnSelected.getModel() ; 
		  coursesUnSelected.setModel(cu_dtm); 
		  cu_dtm.setColumnIdentifiers(columnNames); 
		 
		  for(int i = 1;i<=rowOfunchosen;i++){
		        
		        cs_dtm.addRow(new Object[] {
		        		unChosenCourses.get(i-1).getId(),
		        		unChosenCourses.get(i-1).getName(),
		        		unChosenCourses.get(i-1).getInstitute(),
		        		unChosenCourses.get(i-1).getTeacher(),
		        		unChosenCourses.get(i-1).getGrades(),
		        		unChosenCourses.get(i-1).getNumber(),
		        		unChosenCourses.get(i-1).getPlace(),
		        		unChosenCourses.get(i-1).getShared(),
		        		});   
		          }//display the courses available
		  cs_dtm.fireTableDataChanged();//repaint the table
		  
		  final DefaultTableModel ct_dtm  = (DefaultTableModel)coursesToSelect.getModel() ; 
		  coursesToSelect.setModel(ct_dtm); 
		  ct_dtm.setColumnIdentifiers(columnNames); //��ʼ��һ�ſյı�
		  
		  
		 
		  JButton selectButton = new JButton ("Select");
		  selectButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				int selectRow = coursesUnSelected.getSelectedRow();//��ȡ���е�����   0����1Ҫע����
				// TODO Auto-generated method stub
				tempForSelect.add(selectRow);
				coursesToChoose.add(unChosenCourses.get(selectRow-1));
				ct_dtm.addRow(new Object[] {
						unChosenCourses.get(selectRow-1).getId(),
						unChosenCourses.get(selectRow-1).getName(),
						unChosenCourses.get(selectRow-1).getInstitute(),
						unChosenCourses.get(selectRow-1).getTeacher(),
						unChosenCourses.get(selectRow-1).getGrades(),
						unChosenCourses.get(selectRow-1).getNumber(),
						unChosenCourses.get(selectRow-1).getPlace(),
						unChosenCourses.get(selectRow-1).getShared(),
		        		});
				ct_dtm.fireTableDataChanged();//repaint the table
			}
			
				
				// TODO Auto-generated method stub
				
			  
		  });
		  
		  JButton submit = new JButton("Submit");
		 submit.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				if(connection.handleDeleteCourse(coursesToChoose)){
				JOptionPane.showMessageDialog(null,"Select successfully!");//�ɹ�ɾ��
				for(int i =0;i<tempForSelect.size();i++){
				cs_dtm.addRow(
					new Object[]{
							unChosenCourses.get(i).getId(),
							unChosenCourses.get(i).getName(),
							unChosenCourses.get(i).getInstitute(),
							unChosenCourses.get(i).getTeacher(),
							unChosenCourses.get(i).getGrades(),
							unChosenCourses.get(i).getNumber(),
							unChosenCourses.get(i).getPlace(),
							unChosenCourses.get(i).getShared()//selected increase
					});
				
				cu_dtm.removeRow(tempForSelect.get(i));//available decline
				ct_dtm.removeRow(i+1);//to choose clear
				cu_dtm.fireTableDataChanged();//repaint the table
				cs_dtm.fireTableDataChanged();//repaint the table
				ct_dtm.fireTableDataChanged();//repaint the table
				}
				tempForSelect.clear();//��ʱ�������
			}
			else{
				JOptionPane.showMessageDialog(null,"Fail to Delete!");//ɾ��ʧ��
				for(int i =0;i<tempForSelect.size();i++)
					ct_dtm.removeRow(i+1);//���ѡ����ʱ��
				ct_dtm.fireTableDataChanged();//repaint the table
				tempForSelect.clear();//��ʱ�������
			}
				// TODO Auto-generated method stub
				
			}
			  
		  });
		  
		 
		 
		GridLayout gridLayout = new GridLayout(7,0);//���panel�Ĳ���
		JPanel panelForSelectedTable = new JPanel();
		panelForSelectedTable.add(pane1);
		JPanel panelForUnselectedTable = new JPanel();
		panelForUnselectedTable.add(pane2);
		JPanel panelForToSelectedTable = new JPanel();
		panelForToSelectedTable.add(pane3);
		
		
		JLabel chosenLabel = new JLabel("Course Selected");
		JLabel unChosenLabel = new JLabel("Courses Available");
		JLabel toChooseLabel = new JLabel("Courses to Select");
		JPanel toChoosePanel = new JPanel();
		toChoosePanel.add(toChooseLabel);
		toChoosePanel.add(selectButton);
		JPanel submitPanel = new JPanel();
		submitPanel.add(submit);
		setLayout(gridLayout);
		add(chosenLabel);
		add(panelForSelectedTable);
		add(unChosenLabel);
		add(panelForUnselectedTable);
		add(toChoosePanel);
		add(panelForToSelectedTable);
		add(submitPanel);
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}

}
